package com.rongxun.hbwallet.ui.fragment.loadwallet;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongxun.base.BaseFragment;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.HttpConstants;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.ui.activities.LoadWalletActivity;
import com.rongxun.hbwallet.ui.activities.QRCodeScannerActivity;
import com.rongxun.hbwallet.ui.activities.WebActivity;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.InfoEvent;
import com.rongxun.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dwq on 2018/3/22/022.
 * e-mail:lomapa@163.com
 */

public class LoadWalletByOfficialWalletFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.et_keystore)
    EditText etKeystore;
    @BindView(R.id.et_wallet_pwd)
    EditText etWalletPwd;
    @BindView(R.id.cb_agreement)
    CheckBox cbAgreement;
    @BindView(R.id.btn_load_wallet)
    TextView btnLoadWallet;

    @BindView(R.id.loadwallet_qrcode)
    LinearLayout mQrCode;
    @BindView(R.id.tv_agreement)
    TextView mAgreement;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_load_wallet_by_official_wallet;
    }

    LoadWalletActivity mLoadWalletActivity;

    @Override
    protected void init() {
        EventUtil.register(this);
        mLoadWalletActivity = (LoadWalletActivity) activity;
        cbAgreement.setOnCheckedChangeListener(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventUtil.unregister(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        cbAgreement.setChecked(isChecked);
        btnLoadWallet.setEnabled(isChecked);
        btnLoadWallet.setTextColor(isChecked ? Color.parseColor("#D8B267") : Color.WHITE);
    }

    @OnClick({R.id.btn_load_wallet, R.id.loadwallet_qrcode,R.id.tv_agreement})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_load_wallet:
                String walletPwd = etWalletPwd.getText().toString().trim();
                String keystore = etKeystore.getText().toString().trim();
                boolean verifyWalletInfo = verifyInfo(keystore, walletPwd);
                if (verifyWalletInfo) {

                    mLoadWalletActivity.getPresenter().loadWalletByKeystore(keystore, walletPwd);
                }
                break;

            case R.id.loadwallet_qrcode:
                ActivityUtils.startActivityWithAnimotion(activity,
                        new Intent(activity, QRCodeScannerActivity.class));
                break;
            case R.id.tv_agreement:
                ActivityUtils.startActivityWithAnimotion(activity, new Intent(activity, WebActivity.class).putExtra(IntentKey.WEB_URL, HttpConstants.WEB_ADDRESS.AGREEMENT_URL));
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(InfoEvent event) {
        if (event.id== EventConf.QRCODE_RESULT) {
            etKeystore.setText((String)event.obj);
        }
    }


    private boolean verifyInfo(String keystore, String walletPwd) {
        if (TextUtils.isEmpty(keystore)) {
            ToastUtils.showToast(R.string.load_wallet_by_official_wallet_keystore_input_tip);
            return false;
        } else if (TextUtils.isEmpty(walletPwd)) {
            ToastUtils.showToast(R.string.create_wallet_pwd_input_tips);
            // 同时判断强弱
            return false;
        }
        return true;
    }
}
